package com.example.demo.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.BookType;
import com.example.demo.service.BookTypeService;
import com.example.demo.util.R;

@RestController
@RequestMapping("/book_type")
public class BookTypeController {

  @Autowired
  private BookTypeService bookTypeService;

  @PostMapping("/add")
  public String add(@RequestBody Map<String, String> body) {

    BookType bookType = new BookType(body.get("type"));

    boolean flag = bookTypeService.add(bookType);

    if (flag) {
      return R.success("添加成功").toJSONString();
    } else {
      return R.fail("添加失败").toJSONString();
    }
  }

  @PostMapping("/delete")
  public String delete(@RequestBody Map<String, String> body) {

    boolean flag = bookTypeService.delete(Integer.parseInt(body.get("id")));

    if (flag) {
      return R.success("删除成功").toJSONString();
    } else {
      return R.fail("删除失败").toJSONString();
    }
  }

  @GetMapping("/all")
  public String getAll() {
    return R.success(bookTypeService.getAll()).toJSONString();
  }

}
